//
//  DataPickerView.m
//  PropertyManager
//
//  Created by 楠 on 2018/10/21.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "DataPickerView.h"

@implementation DataPickerView

+(instancetype)shareView
{
    static DataPickerView * view = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        view = [[DataPickerView alloc] initWithFrame:CGRectMake(0, 0 , WIDTH, HEIGHT)];
    });
    return view;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = SFM239;
        [self addSubview:self.bgView];
        [self.bgView addSubview:self.cancleBtn];
        [self.bgView addSubview:self.sureBtn];
        [self.bgView addSubview:self.datePicker];
        self.datePicker.datePickerMode = UIDatePickerModeDate;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
//        if(self.str)
//        {
//            NSDate *dateFromString = [[NSDate alloc] init];
//            dateFromString = [dateFormatter dateFromString:self.str];
//            if (dateFromString) [self.datePicker setDate:dateFromString];
//        }
    }
    return self;
}

-(UIView *)bgView
{
    if (_bgView == nil) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - kHomeLineH - 260, WIDTH, 260)];
        _bgView.backgroundColor = SFM242;
    }
    return _bgView;
}

-(UIDatePicker *)datePicker
{
    if (_datePicker == nil)
    {
        _datePicker   = [[UIDatePicker alloc] init];
        _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        _datePicker.backgroundColor = [UIColor whiteColor];
        // UIDatePicker默认高度216
        _datePicker.frame = CGRectMake(0, 44 , WIDTH , _datePicker.frame.size.height);
    }
    return _datePicker;
}

-(void)hiddenView
{
    [UIView animateWithDuration:0.5 animations:^{
        [self removeFromSuperview];
    }];
}

-(void)showView
{
    [UIView animateWithDuration:0.5 animations:^{
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hiddenView];
}

-(UIButton *)cancleBtn
{
    if (_cancleBtn == nil) {
        _cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
        [_cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancleBtn setTitleColor:SFM153 forState:UIControlStateNormal];
        [_cancleBtn addTarget:self action:@selector(cancleBtnClcik) forControlEvents:UIControlEventTouchUpInside];
        _cancleBtn.titleLabel.font = font(16);
    }
    return _cancleBtn;
}

-(void)cancleBtnClcik
{
    [self hiddenView];
}

-(UIButton *)sureBtn
{
    if (_sureBtn == nil) {
        _sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH - 60, 0, 60, 44)];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:SFMBlueColor forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(sureBtnClcik) forControlEvents:UIControlEventTouchUpInside];
        _sureBtn.titleLabel.font = font(16);
    }
    return _sureBtn;
}

-(void)sureBtnClcik
{
    NSDate *selectData = self.datePicker.date;
    self.sendChooseDateBlock(selectData);
    [self hiddenView];
}


@end
